package com.hbnu.Service.Impl;

import com.hbnu.Dao.DormManagerDao;
import com.hbnu.Dao.Impl.DormManagerDaoImp;
import com.hbnu.Dao.Impl.UserDaoImpl;
import com.hbnu.Dao.UserDao;
import com.hbnu.Pojo.DormBuild;
import com.hbnu.Pojo.PageModel;
import com.hbnu.Pojo.User;
import com.hbnu.Service.UserService;

import java.util.Collections;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private DormManagerDao dormManagerDao = new DormManagerDaoImp();
    public User findUserByStuCodeAdnPassword(String stuCode, String password) {

        return userDao.findUserByStuCodeAndPassword(stuCode, password);
    }

    @Override
    public List<User> findAllManager() {



        List<User> userList = userDao.findManager();

        if (null != userList) {
            for (User user : userList) {
                Integer userId = user.getId();
                List<DormBuild> dormBuilds = userDao.findDormBuildsByUserId(userId);
                user.setDormBuilds(dormBuilds);
            }
        }


        return userList;
    }

    @Override
    public List<User> findAllManager(String searchType, String keyword) {


        List<User> userList = userDao.findManager(searchType, keyword);

        if (null != userList) {
            for (User user : userList) {
                Integer userId = user.getId();
                List<DormBuild> dormBuilds = userDao.findDormBuildsByUserId(userId);
                user.setDormBuilds(dormBuilds);
            }
        }


        return userList;

    }

    @Override
    public void savaManager(User user, String[] dormId) {
        String managerStuCodeMax = userDao.findManagerStuCodeMax();

        user.setStuCode(managerStuCodeMax);

        Integer userId = userDao.savaManager(user);

        dormManagerDao.savaManagerAndBuild(userId, dormId);

    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public void updateManager(User user, String[] dormId) {
        userDao.updateManager(user, dormId);
    }

    @Override
    public void updateManager(User user) {
        userDao.updateManager(user);

    }

    @Override
    public List<User> findStudent(String dormId, String searchType, String keyword, User user, PageModel pageModel) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT user.*, build.name AS buildName, build.* " +
                " FROM tb_user user " +
                " LEFT JOIN tb_dormbuild build ON build.id = user.dormBuildId" +
                " WHERE user.role_id = 2 ");
        if (keyword != null && !keyword.isEmpty() && "name".equals(searchType)) {
            sql.append(" AND user.name LIKE '%").append(keyword.trim()).append("%'");
        }
        else if (keyword != null && !keyword.isEmpty() && "stuCode".equals(searchType)) {
           sql.append(" AND user.stu_code ='").append(keyword.trim()).append("%'");
        }
        else if (keyword != null && !keyword.isEmpty() && "dormCode".equals(searchType)) {
            sql.append(" AND user.dorm_code ='").append(keyword.trim()).append("'");
        }
        else if (keyword != null && !keyword.isEmpty() && "sex".equals(searchType)) {
            sql.append(" and user.sex = '").append(keyword.trim()).append("'");
        }
        else if (keyword != null && !keyword.isEmpty() && "tel".equals(searchType)) {
            sql.append(" AND user.tel = '").append(keyword.trim()).append("'");
        }

        if (dormId != null && !dormId.isEmpty()) {
            sql.append(" and user.dormBuildId ='").append(dormId).append("'");
        }

        if (user.getRoleId() == 1) {
            sqlFindBuilds(user, sql);
        }
        sql.append(" limit ").append(pageModel.getStartNum()).append(" , ").append(pageModel.getPageSize());

        return userDao.findStudent(sql.toString());
    }

    @Override
    public Integer findTotalNum(String dormId, String searchType, String keyword, User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT user.*, build.name AS buildName, build.* " +
                " FROM tb_user user " +
                " LEFT JOIN tb_dormbuild build ON build.id = user.dormBuildId" +
                " WHERE user.role_id = 2 ");
        if (keyword != null && !keyword.isEmpty() && "name".equals(searchType)) {
            sql.append(" AND user.name LIKE '%").append(keyword.trim()).append("%'");
        }
        else if (keyword != null && !keyword.isEmpty() && "stuCode".equals(searchType)) {
            sql.append(" AND user.stu_code ='").append(keyword.trim()).append("%'");
        }
        else if (keyword != null && !keyword.isEmpty() && "dormCode".equals(searchType)) {
            sql.append(" AND user.dorm_code ='").append(keyword.trim()).append("'");
        }
        else if (keyword != null && !keyword.isEmpty() && "sex".equals(searchType)) {
            sql.append(" and user.sex = '").append(keyword.trim()).append("'");
        }
        else if (keyword != null && !keyword.isEmpty() && "tel".equals(searchType)) {
            sql.append(" AND user.tel = '").append(keyword.trim()).append("'");
        }

        if (dormId != null && !dormId.isEmpty()) {
            sql.append(" and user.dormBuildId ='").append(dormId).append("'");
        }

        if (user.getRoleId() == 1) {
            sqlFindBuilds(user, sql);
        }

        return userDao.findTotalNum(sql.toString());

    }

    @Override
    public User findByStuCode(String stuCode) {
        return userDao.findByStuCode(stuCode);
    }

    @Override
    public void updateStudent(User studentUpdate) {
        userDao.updateStudent(studentUpdate);
    }

    @Override
    public void saveStudent(User user) {
        userDao.saveStudent(user);
    }

    @Override
    public String findManagerStuCodeMax() {
        return userDao.findManagerStuCodeMax();
    }

    @Override
    public User findByUserIdAndManager(int id, User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from tb_user as user where user.id =  ").append(id);

        if (user.getRoleId() != 1 && user.getRoleId() == 1) {
            sqlFindBuilds(user, sql);
        }

        return userDao.findByUserIdAndManager(sql.toString());
    }

    private void sqlFindBuilds(User user, StringBuffer sql) {
        List<DormBuild> dormBuilds = dormManagerDao.findByUserId(user.getId());

        sql.append(" and user.dormBuildId in (");
        for (DormBuild dormBuild : dormBuilds) {
            sql.append(dormBuild.getId()).append(",");
        }

        sql.deleteCharAt(sql.length() - 1);
        sql.append(")");
    }


    public static class DormManagerServiceImp {
    }
}
